package com.wing.portal.aggregation;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Map;

import com.wing.common.services.HtmlStaticTemplateService;
import com.wing.common.util.ApplicationPath;
import com.wing.common.util.SpringDI;
import com.wing.portal.aggregation.portletinfo.PortletInfoFactroy;
import com.wing.sys.service.AuthManager;
import com.wing.sys.service.UserManager;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
 * 
 * @author wyf
 * 
 */
public class PortletFragment extends AbstractFragment {

	public PortletFragment(Map<String,String> id) {
		super(id);
	}

	@Override
	public Collection<Fragment> getChildFragments() {
		return null;
	}

	@Override
	public void postService() {
	}

	@Override
	public void preService() {
	}

	@Override
	public String selfService() {
		String portletype = this.getParam().get("portletype");
		Map root = PortletInfoFactroy.createPortletInfo(portletype).loadPortletInfo(this.getParam());
		root.putAll(this.getParam());
		Integer width = (Integer)root.get("portletWidth")==null?0:(Integer)root.get("portletWidth");
		Integer height = (Integer)root.get("portletWidth")==null?0:(Integer)root.get("portletheight");
		String strwidth = width==null?"100%":width==0?"100%":width+"px";
		String strheight = height==null?"100%":height==0?"100%":height+"px";
		String templateFilePath =  PortletInfoFactroy.createPortletInfo(portletype).findTemplateFile(this.getParam().get("portletid"));
		StringWriter sw = new StringWriter();
		String templatesRoot = ApplicationPath.getParameter("appRoot")
				+ File.separator + "site" + File.separator
				+ this.getParam().get("sitePath") + File.separator
				+ "framework" + File.separator + "template";

		UserManager usermanager = (UserManager) SpringDI.getBean("userManager");
		AuthManager authManager = (AuthManager) SpringDI
		.getBean("authManager");
		Configuration cfg = new Configuration();
		try {
			sw.write("<div style=\"width:"+strwidth+";height:"+strheight+"\" id = \"" + this.getParam().get("portletid")
					+ "\" class=\"portlet\">");
			if ((usermanager.isAdminByUserId(this.getParam().get("userId"))||authManager.isHasAuth("site",this.getParam().get("userId"),this.getParam().get("siteId")))
					&& this.getParam().get("isLayout").equals("1")) {
				sw.write("<div style=\"width:"+strwidth+";height:"+strheight+"\" class=\"portlet-header\">"
						+ HtmlStaticTemplateService.getHtml("portletHead")
								.replaceAll("\\$title",
										(String) root.get("title")).replaceAll(
										"\\$portletInstanceId",
										this.getParam().get("portletid"))
								.replaceAll("\\$portletype",
										this.getParam().get("portletype"))
						+ "</div>");
			}
			sw.write("<div style=\"width:"+strwidth+";height:"+strheight+"\" class=\"portlet-content\" >");

			cfg.setDirectoryForTemplateLoading(new File(templatesRoot));
			Template temp = cfg.getTemplate(templateFilePath);
			temp.process(root, sw);
			sw.write("</div>");
			sw.write("</div>");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		} finally {
		}
		return sw.toString();
	}

	@Override
	public void addChild(Fragment child) {

	}
}
